package cn.iocoder.yudao.framework.operatelog.api.controller;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.api.controller.vo.*;
import cn.iocoder.yudao.framework.operatelog.api.convert.permission.ApiPermissionConvert;
import cn.iocoder.yudao.framework.operatelog.api.dal.dataobject.permission.ApiPermissionDO;
import cn.iocoder.yudao.framework.operatelog.api.service.permission.ApiPermissionService;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

/**
 * @author Acer
 */
@Api(tags = "")
@RestController
@RequestMapping("/api/permission")
@Validated
public class ApiPermissionController {

    @Resource
    private ApiPermissionService permissionService;

    @PostMapping("/create")
    @ApiOperation("创建")
    @PreAuthorize("@ss.hasPermission('api:permission:create')")
    public CommonResult<Integer> createPermission(@Valid @RequestBody ApiPermissionCreateReqVO createReqVO) {
        return success(permissionService.createPermission(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新")
    @PreAuthorize("@ss.hasPermission('api:permission:update')")
    public CommonResult<Boolean> updatePermission(@Valid @RequestBody ApiPermissionUpdateReqVO updateReqVO) {
        permissionService.updatePermission(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('api:permission:delete')")
    public CommonResult<Boolean> deletePermission(@RequestParam("id") Integer id) {
        permissionService.deletePermission(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('api:permission:query')")
    public CommonResult<ApiPermissionRespVO> getPermission(@RequestParam("id") Integer id) {
        ApiPermissionDO permission = permissionService.getPermission(id);
        return success(ApiPermissionConvert.INSTANCE.convert(permission));
    }

    @GetMapping("/list")
    @ApiOperation("获得列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('api:permission:query')")
    public CommonResult<List<ApiPermissionRespVO>> getPermissionList(@RequestParam("ids") Collection<Integer> ids) {
        List<ApiPermissionDO> list = permissionService.getPermissionList(ids);
        return success(ApiPermissionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得分页")
    @PreAuthorize("@ss.hasPermission('api:permission:query')")
    public CommonResult<PageResult<ApiPermissionRespVO>> getPermissionPage(@Valid ApiPermissionPageReqVO pageVO) {
        PageResult<ApiPermissionDO> pageResult = permissionService.getPermissionPage(pageVO);
        return success(ApiPermissionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出 Excel")
    @PreAuthorize("@ss.hasPermission('api:permission:export')")
    @OperateLog(type = EXPORT)
    public void exportPermissionExcel(@Valid ApiPermissionExportReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<ApiPermissionDO> list = permissionService.getPermissionList(exportReqVO);
        // 导出 Excel
        List<ApiPermissionExcelVO> datas = ApiPermissionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, ".xls", "数据", ApiPermissionExcelVO.class, datas);
    }

}
